using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Data.Sql;
using System.Data.SqlClient;

namespace Database
{
   public class TinTuc
    {
        private int matintuc;

        public int Matintuc
        {
            get { return matintuc; }
            set { matintuc = value; }
        }
        private string tieude;

        public string Tieude
        {
            get { return tieude; }
            set { tieude = value; }
        }
        private string mota;

        public string Mota
        {
            get { return mota; }
            set { mota = value; }
        }
        private string chitiet;

        public string Chitiet
        {
            get { return chitiet; }
            set { chitiet = value; }
        }
        private DateTime ngaydang;

        public DateTime Ngaydang
        {
            get { return ngaydang; }
            set { ngaydang = value; }
        }
        private string hinhanh;

        public string Hinhanh
        {
            get { return hinhanh; }
            set { hinhanh = value; }
        }
        private string tennhanvien;

        public string Tennhanvien
        {
            get { return tennhanvien; }
            set { tennhanvien = value; }
        }
       public TinTuc()
       {
       }
       public TinTuc(int matintuc, string tieude, string mota, string chitiet, DateTime ngaydang, string hinhanh,string tennhanvien)
       {
           this.matintuc = matintuc;
           this.tieude = tieude;
           this.mota = mota;
           this.chitiet = chitiet;
           this.ngaydang = ngaydang;
           this.hinhanh = hinhanh;
           this.tennhanvien = tennhanvien;
       }
       public static DataSet DSTT()
       {
           DataSet ds = new DataSet();
           string query = "select * from tintuc";
           sqlConnection db = new sqlConnection();
           SqlDataAdapter sda = new SqlDataAdapter(query,db.GetConnect());
           sda.Fill(ds);
           return ds;
       }
       public DataSet DSTinTuc()
       {
           DataSet ds = new DataSet();
           string query = "select * from tintuc";
           sqlConnection db = new sqlConnection();
           SqlDataAdapter sda = new SqlDataAdapter(query, db.GetConnect());
           sda.Fill(ds);
           return ds;
       }
       public bool ThemTT(string tieude, string mota, string chitiet,DateTime ngaydang, string hinhanh,string tennhanvien)
       {
           bool kq = true;

           try
           {
               string query = "insert into TinTuc(tieude,mota,chitiet,ngaydang,hinhanh,tennhanvien) values(N'{0}',N'{1}',N'{2}','{3}','{4}','{5}')";
               query = String.Format(query,tieude,mota,chitiet,ngaydang,hinhanh,tennhanvien);
               sqlConnection db = new sqlConnection();
               SqlCommand cmd = new SqlCommand(query,db.GetConnect());
               return cmd.ExecuteNonQuery() > 0;
           }
           catch
           {
               kq = false;
           }

           return kq;
       }
       public bool XoaTT(int matintuc)
       {
           bool kq = true;

           try
           {
               string query = "Delete TinTuc where matintuc='{0}'";
               query = String.Format(query,matintuc);
               sqlConnection db = new sqlConnection();
               SqlCommand cmd = new SqlCommand(query, db.GetConnect());
               cmd.ExecuteNonQuery();
           }
           catch
           {
               kq = false;
           }

           return kq;
       }
       public bool CapNhatTT(int matintuc,string tieude, string mota, string chitiet, DateTime ngaydang, string hinhanh,string tennhanvien)
       {
           bool kq = true;

           try
           {
               string query = "update TinTuc set tieude='{0}',mota='{1}',chitiet='{2}',ngaydang='{3}',hinhanh='{4}',tennhanvien='{5}' where matintuc='{6}'";
               query = String.Format(query,tieude,mota,chitiet,ngaydang,hinhanh,tennhanvien,matintuc);
               sqlConnection db = new sqlConnection();
               SqlCommand cmd = new SqlCommand(query,db.GetConnect());
              return cmd.ExecuteNonQuery()>0;
           }
           catch
           {
               kq = false;
           }

           return kq;
       }
       public TinTuc ThongTinTT(int matintuc)
       {
           string query = "select * from TinTuc where matintuc=" + matintuc;
           sqlConnection db = new sqlConnection();
           SqlCommand sc = new SqlCommand(query, db.GetConnect());
           SqlDataReader reader = sc.ExecuteReader();
           TinTuc tt = new TinTuc();
           while (reader.Read())
           {
               tt.matintuc = reader.GetInt32(0);
               tt.tieude = reader.GetString(1);
               tt.mota = reader.GetString(2);
               tt.chitiet = reader.GetString(3);
               tt.ngaydang = reader.GetDateTime(4);
               tt.hinhanh = reader.GetString(5);
               tt.tennhanvien = reader.GetString(6);

           }
           return tt;
       }
       public DataTable THTinTuc()
       {
           DataTable ds = new DataTable();
           try
           {
               string query = "select * from tintuc";
               query = string.Format(query);
               sqlConnection db = new sqlConnection();
               SqlDataAdapter sda = new SqlDataAdapter(query, db.GetConnect());
               sda.Fill(ds);
           }
           catch
           {
               ds = null;
           }
           return ds;
       }
       public DataTable TinTucTheoMa(int matintuc)
       {
           DataSet ds = new DataSet();
           try
           {
               string query = string.Format("select * from tintuc where matintuc={0}", matintuc);
               sqlConnection db = new sqlConnection();
               SqlDataAdapter sda = new SqlDataAdapter(query, db.GetConnect());
               sda.Fill(ds);
           }
           catch
           {
               ds = null;
           }
           return ds.Tables[0];
       }
    }
    
}
